﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data;
using DTO;
using UTIL;


namespace DAO
{
    public class KhuyenMaiDao
    {
        public static bool ThemKhuyenMai(KhuyenMaiDto one)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_themKhuyenMai");
                StoreUtilities.AddInputPara(cmd, "@ngaybatdau", SqlDbType.DateTime, one.NgayBatDau);
                StoreUtilities.AddInputPara(cmd, "@ngayketthuc", SqlDbType.DateTime, one.NgayKetThuc);
                StoreUtilities.AddInputPara(cmd, "@hinhthuc", SqlDbType.Int, one.HinhThuc);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
                throw ex;

            }
            return result;
        }

        public static bool XoaKhuyenMai(int id)
        {
            bool result = false;
            try
            {
                // tao ket noi CSDL 
                SqlConnection cnn = DataConnector.MakeConnection();

                // goi proc thuc thi them loai nguoi dung
                SqlCommand cmd = StoreUtilities.CreateStoreCommand(cnn, "sp_xoaKhuyenMai");
                StoreUtilities.AddInputPara(cmd, "@ID", SqlDbType.Int, id);

                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                    result = true;

                // dong ket noi
                cnn.Close();
            }
            catch (Exception ex)
            {
                // bat loi 
            }
            return result;
        }
        public static List<DTO.KhuyenMaiDto> LayDanhSachKhuyenMai()
        {
            SqlConnection cnn = DataConnector.MakeConnection();
            string sql = "Select * From KhuyenMai";
            SqlCommand cmd = new SqlCommand(sql, cnn);
            SqlDataReader dr = cmd.ExecuteReader();
            List<DTO.KhuyenMaiDto> list = new List<KhuyenMaiDto>();
            DTO.KhuyenMaiDto km=null;
            while (dr.Read())
            {
                km = new KhuyenMaiDto();
                km.ID = (int)dr["ID"];
                km.NgayBatDau = (DateTime)dr["ngaybatdau"];
                km.NgayKetThuc = (DateTime)dr["ngayketthuc"];
                km.HinhThuc = (int)dr["hinhthuc"];
                DateTime now = DateTime.Now;
                if ((now >= km.NgayBatDau && now <= km.NgayKetThuc)&& km.HinhThuc!=0)
                    list.Add(km);
            }
            cnn.Close();

            return list;
        }
        public static KhuyenMaiDto LayKhuyenMaiTheoID(int id)
        {
            SqlConnection cnn = DataConnector.MakeConnection();
            string sql = "Select * From KhuyenMai where ID="+id;
            SqlCommand cmd = new SqlCommand(sql, cnn);
            SqlDataReader dr = cmd.ExecuteReader();
           
            DTO.KhuyenMaiDto km = null;
            if (dr.Read())
            {
                km = new KhuyenMaiDto();
                km.ID = (int)dr["ID"];
                km.NgayBatDau = (DateTime)dr["ngaybatdau"];
                km.NgayKetThuc = (DateTime)dr["ngayketthuc"];
                km.HinhThuc = (int)dr["hinhthuc"];
               
            }
            cnn.Close();

            return km;
        }


    }
}
